package com.and9.mchen.mcmusic.dao.factory;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.and9.mchen.mcmusic.dao.IMusicStyleDao;
import com.and9.mchen.mcmusic.entity.MusicStyle;
import com.and9.mchen.mcmusic.exception.DaoException;

public final class MusicStyleManager {

	private final static Log log = LogFactory.getLog(MusicStyleManager.class);
	
	private static IMusicStyleDao musicStyleDao=null;
	
	private static List<MusicStyle> musicStyles=null;
	
	static{
		
		musicStyleDao=MusicStyleDaoFactory.getInstance().getMusicStyleDao();
		try{
			musicStyles=musicStyleDao.getAllMusicStyles();
		}catch(DaoException e){
			log.error("init all music styles failure", e);
			musicStyles=new LinkedList<MusicStyle>();
		}
		
	}
	
	public static List<MusicStyle> getAllMusicStyle(){
		return musicStyles;
	}
	
	public static synchronized void reloadAllMusicStyle(){
		
		try {
			musicStyles=musicStyleDao.getAllMusicStyles();
		} catch (DaoException e) {
			log.error("reload all music styles failure", e);
			musicStyles=new LinkedList<MusicStyle>();
		}
		
	}
	
	public static MusicStyle findMusicStyleByMusicStyleId(long id){
		
		for(MusicStyle musicStyle : musicStyles){
			if(musicStyle.getId().equals(id)){
				return musicStyle;
			}
		}
		
		return null;
	}
	
	
	public static MusicStyle findMusicStyleByMusicStyleName(String styleName){
		
		for(MusicStyle musicStyle : musicStyles){
			if(musicStyle.getStyleName().equals(styleName)){
				return musicStyle;
			}
		}
		
		return null;
		
	}
}
